<template>
  <div class="view-fill">
    <div
      v-show="!assetApplicationDetailInfo.visible"
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :btn-list="fullInfo.btnList"
        :needReturn="!$route.query.processInstanceId"
        @returnBack="$emit('returnModify')"
      >
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <!-- form -->
            <page-form
              class="paddingBox order-model-formBox flex_wrap_start_start"
              v-model:ref-obj="formInfo.ref"
              :disabled="formInfo.disabled"
              :data="formInfo.data"
              :field-list="formInfo.fieldList"
              :rules="formInfo.rules"
              :label-width="formInfo.labelWidth"
            >
              <template v-slot:form-deptName>
                <el-input
                  v-if="isSpecialEdit === 1"
                  v-model="formInfo.data.deptName"
                  disabled
                ></el-input>
                <el-input
                  v-else
                  v-model="formInfo.data.deptName"
                  class="not-mongolia"
                  type="input"
                  :disabled="true"
                  placeholder="请选择部门"
                >
                  <template #append>
                    <el-icon @click="openDeptWindow"><MoreFilled /></el-icon>
                  </template>
                </el-input>
              </template>
              <template v-slot:form-receiveAddressValue>
                <el-input
                  v-if="isSpecialEdit === 1"
                  v-model="formInfo.data.receiveAddressValue"
                  disabled
                ></el-input>
                <el-input
                  v-else
                  v-model="formInfo.data.receiveAddressValue"
                  class="not-mongolia"
                  type="input"
                  :disabled="true"
                  placeholder="请选择收货地址"
                >
                  <template #append>
                    <el-icon @click="openReceiveAddressWindow"
                      ><MoreFilled
                    /></el-icon>
                  </template>
                </el-input>
              </template>
            </page-form>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <!-- 条件栏 -->
            <page-filter
              v-model:query="filterInfo.query"
              :filter-list="filterInfo.list"
            >
              <template v-slot:filter-addProduct>
                <el-button
                  :disabled="!canAddProduct"
                  v-if="
                    !(
                      formInfo.data.contractId && formInfo.data.contractId != -2
                    )
                  "
                  @click="addProduct"
                  >添加资产</el-button
                >
              </template>
              <template v-slot:filter-addContractProduct>
                <el-button
                  :disabled="!canAddProduct"
                  v-if="
                    formInfo.data.contractId && formInfo.data.contractId != -2
                  "
                  @click="addContractProduct"
                  >添加合同资产</el-button
                >
              </template>
              <template v-slot:filter-removeProduct>
                <el-button :disabled="!canRemoveProduct" @click="removeProduct"
                  >删除资产</el-button
                >
              </template>
              <!-- <template v-slot:filter-changeAllSupplier>
                <el-button
                  :disabled="!canChangeAllSupplier"
                  v-if="
                    !(
                      formInfo.data.contractId && formInfo.data.contractId != -2
                    )
                  "
                  @click="changeAllSupplier"
                  >批量切换供应商</el-button
                >
              </template> -->
            </page-filter>
            <div class="flex_full_item">
              <el-form ref="tableForm" :model="tableInfo" style="width: 100%">
                <!-- 表格 -->
                <page-table
                  class="table-scroll-hidden"
                  :refresh="tableInfo.refresh"
                  :init-curpage="tableInfo.initCurpage"
                  v-model:data="tableInfo.data"
                  :pager="false"
                  :summary="true"
                  :summary-field-list="tableInfo.summaryFieldList"
                  :tab-index="false"
                  :query="filterInfo.query"
                  :field-list="tableInfo.fieldList"
                  :handle="tableInfo.handle"
                  :row-class-name="tableRowClassName"
                  :isDynamic="true"
                  @handleEvent="handleEvent"
                >
                  <template v-slot:col-assetApplicationNo="scope">
                    <span
                      class="slip-edit"
                      @click="showAssetApplicationDetail(scope.row)"
                      >{{ scope.row.assetApplicationNo }}</span
                    >
                  </template>
                  <template v-slot:col-qty="scope">
                    <el-form-item
                      v-if="scope.$index >= 0"
                      :prop="`data.${scope.$index}.qty`"
                      :rules="
                        scope.$index >= 0
                          ? [
                              {
                                validator: $validate.positive,
                                trigger: 'blur',
                              },
                            ]
                          : []
                      "
                    >
                      <el-input-number
                        style="width: 100%"
                        v-model.lazy="scope.row.qty"
                        clearable
                        controls-position="right"
                        :precision="0"
                        :min="0"
                        :max="scope.row.pendPurchQty || 99999999"
                      />
                    </el-form-item>
                  </template>
                  <template v-slot:col-price="scope">
                    <el-form-item
                      v-if="scope.$index >= 0"
                      :prop="`data.${scope.$index}.price`"
                      :rules="
                        scope.$index >= 0
                          ? [
                              {
                                validator: $validate.positive,
                                trigger: 'blur',
                              },
                            ]
                          : []
                      "
                    >
                      <el-input-number
                        style="width: 100%"
                        v-model.lazy="scope.row.price"
                        clearable
                        :disabled="
                          !!(
                            formInfo.data.contractId &&
                            formInfo.data.contractId != -2
                          )
                        "
                        controls-position="right"
                        :precision="systemConfig.qty_show_precision"
                        :min="0"
                        :max="99999999"
                      />
                    </el-form-item>
                  </template>
                </page-table>
              </el-form>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <!--资产选择-->
    <select-equipment-archives
      v-if="selectEquipmentInfo.visible"
      v-model:show-dialog="selectEquipmentInfo.visible"
      :query="selectEquipmentInfo.query.queryMap"
      :query-params="selectEquipmentInfo.query.queryParams"
      :check-box="true"
      @callback="callBackEquipment"
    />
    <!-- 选择收货地址 -->
    <select-receive-address
      v-if="selectReceiveAddressInfo.visible"
      v-model:show-dialog="selectReceiveAddressInfo.visible"
      :query="selectReceiveAddressInfo.query"
      :check-box="false"
      :highlight-row="true"
      @callback="selectReceiveAddressCallback"
    />
    <!-- 选择部门 -->
    <select-dept
      v-if="selectDeptInfo.visible"
      v-model:show-dialog="selectDeptInfo.visible"
      :query="selectDeptInfo.query"
      :check-box="false"
      :highlight-row="true"
      :saveEmpty="true"
      @callback="selectDeptCallback"
    />
    <select-supplier
      v-if="supplierInfo.visible"
      :check-box="false"
      :highlight-row="true"
      v-model:show-dialog="supplierInfo.visible"
      :query="supplierInfo.query"
      :api="supplierInfo.api"
      @callback="selectSupplierCallback"
    />
    <select-supplier
      v-if="allSupplierInfo.visible"
      :check-box="false"
      :highlight-row="true"
      v-model:show-dialog="allSupplierInfo.visible"
      :query="allSupplierInfo.query"
      @callback="selectAllSupplierCallback"
    />
    <!--资产申论选择-->
    <select-asset-application
      v-if="selectAssetApplicationInfo.visible"
      v-model:show-dialog="selectAssetApplicationInfo.visible"
      :query="selectAssetApplicationInfo.query.queryMap"
      :query-params="selectAssetApplicationInfo.query.queryParams"
      :check-box="false"
      :highlightRow="true"
      :save-empty="true"
      @callback="callBackAssetApplication"
    />
    <!--资产申论选择-->
    <select-contract-detail
      v-if="selectContractDetailInfo.visible"
      v-model:show-dialog="selectContractDetailInfo.visible"
      :query="selectContractDetailInfo.query.queryMap"
      :query-params="selectContractDetailInfo.query.queryParams"
      :check-box="true"
      @callback="callBackContractDetail"
    />
    <!-- 资产申购论证 -->
    <asset-application-detail
      v-if="assetApplicationDetailInfo.visible"
      :id="assetApplicationDetailInfo.id"
      v-model:visible="assetApplicationDetailInfo.visible"
      :title="assetApplicationDetailInfo.title"
    />
  </div>
</template>

<script setup name="assetPurchOrderModify">
import {
  createAssetPurchOrder,
  createAssetPurchOrderAndSave,
  updateAssetPurchOrder,
  getAssetPurchOrderById,
  getAssetPurchOrdersDetailCustomizeListAll,
  getAssetPurchOrdersDetailByContractId,
  methodAssetPurchOrder,
} from '@/api/asset.js'
import { getReceiveAddressPage, getSupplierProductPage } from '@/api/core'
import { getCustomizeSetting } from '@/api/customize'
import AssetApplicationDetail from '../assetApplication/details'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps([
  'tableKey',
  'visible',
  'title',
  'orderId',
  'contractId',
  'contractNo',
  'isSpecialEdit',
  'flowFormType',
  'id',
])

const formRefs = ref([])
const formInfo = ref({
  ref: null,
  data: {
    deptName: proxy.$store.state.user.userInfo.dept
      ? proxy.$store.state.user.userInfo.dept.name
      : '',
    deptId: proxy.$store.state.user.userInfo.dept
      ? proxy.$store.state.user.userInfo.dept.id
      : '',
    supplierId: '',
    supplierName: '',
    purchProductType: 2,
    contractId: props.contractId ? props.contractId : -2,
    contractNo: props.contractNo ? props.contractNo : '',
    receiveAddressValue: '',
    createUserName: proxy.$store.state.user.userInfo.user
      ? proxy.$store.state.user.userInfo.user.name
      : '',
  },
  fieldList: [
    { label: '部门', value: 'deptName', type: 'slot' },
    {
      label: '创建人',
      value: 'createUserName',
      type: 'input',
      disabled: true,
    },
    { label: '收货地址', value: 'receiveAddressValue', type: 'slot' },
    {
      label: '备注',
      value: 'remark',
      type: 'textarea',
    },
  ],
  rules: {
    deptName: [{ required: true, message: '请选择部门', trigger: 'change' }],
    receiveAddressValue: [
      { required: true, message: '请选择收货地址', trigger: 'change' },
    ],
    supplierName: [
      { required: true, message: '请选择供应商', trigger: 'change' },
    ],
  },
  labelWidth: '120px',
})
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {
      needPendPurchQty: true,
    },
  },
  list: [
    { type: 'slot', label: '添加产品', value: 'addProduct' },
    { type: 'slot', label: '添加合同产品', value: 'addContractProduct' },
    { type: 'slot', label: '删除产品', value: 'removeProduct' },
    { type: 'slot', label: '选择所有供应商', value: 'changeAllSupplier' },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'price', show: 'false' },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [],
  applicationFieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '150',
    btList: [
      {
        label: '切换供应商',
        event: changeSupplier,
        show: true,
        ifRender: ifRender,
      },
      {
        label: '资产申购论证导入',
        event: addAssetApplication,
        show: true,
        ifRender: ifRender,
      },
      {
        label: '删除',
        popconfirmText: '删除后将改变数据,是否继续？',
        event: removeProduct,
        show: true,
      },
    ],
  },
})
const fullInfo = ref({
  btnList: [
    {
      label: '保存并提交',
      event: saveAndSubmit,
      show:
        !props.orderId &&
        !props.id &&
        proxy.$permission('assetPurchOrder:saveAndSubmit') &&
        !props.flowFormType,
    },
    { label: '保存', event: save, show: !props.flowFormType },
  ],
})
const selectEquipmentInfo = ref({
  visible: false,
  query: {
    queryMap: {
      id: {
        queryType: 'NOT_IN',
        queryCategory: 'AND',
        value: '',
      },
      supplierId: {
        queryType: 'IN',
        queryCategory: 'AND',
        value: '',
      },
    },
    queryParams: {},
  },
})
const selectContractDetailInfo = ref({
  visible: false,
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: '',
      },
      productId: {
        queryType: 'NOT_IN',
        queryCategory: 'AND',
        value: '',
      },
    },
    queryParams: {},
  },
})
const selectReceiveAddressInfo = ref({
  visible: false,
  query: {},
})
const selectAssetApplicationInfo = ref({
  visible: false,
  query: {
    queryMap: {
      id: {
        queryType: 'NOT_IN',
        queryCategory: 'AND',
        value: '',
      },
      status: {
        queryType: 'IN',
        queryCategory: 'AND',
        value: '6',
      },
    },
    queryParams: {
      productId: '',
      pendPurchQtyFlag: true,
      purchOrderId: '',
    },
  },
})
const selectDeptInfo = ref({
  visible: false,
  query: {
    deptAuth: 'id',
  },
})
const supplierInfo = ref({
  visible: false,
  query: {
    id: '',
    searchType: 'org',
    searchField: 'supplierId',
    supplierAuth: 'supplierId',
  },
  api: getSupplierProductPage,
})
const allSupplierInfo = ref({
  visible: false,
  query: {},
})
const assetApplicationDetailInfo = ref({
  visible: false,
  id: '',
  title: '申论详情',
})
const index = ref('')
const tmpData = ref([]) //用于显示取值
const showSelectOrder = ref(false)
const mergedPurchIds = ref('')
const disabledTemplateBtn = ref(false)
const defaultSelect = ref([])
const systemConfig = ref(proxy.$store.state.permission.systemConfig)
const licencesSystemConfig = ref('')

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})
const isUpdate = computed(() => {
  return formInfo.value.data.id !== undefined
})
const canAddProduct = computed(() => {
  return formInfo.value.data.deptId && props.isSpecialEdit !== 1
})
const canRemoveProduct = computed(() => {
  return tableInfo.value.selections.length && props.isSpecialEdit !== 1
})
const canChangeAllSupplier = computed(() => {
  return tableInfo.value.data && props.isSpecialEdit !== 1
})

watch(
  () => tableInfo.value.data,
  () => {
    tableInfo.value.data.forEach((row) => {
      if (row.qty && row.price) {
        row.sumAmount = proxy.$fn.accMul(
          row.qty,
          parseFloat(row.price == null ? 0 : row.price)
        )
      }
    })
  },
  {
    deep: true,
  }
)

onMounted(() => {
  formRefs.value = [proxy.$refs['tableForm'], formInfo.value.ref]
  //请求默认地址并回填
  getReceiveAddressPage({ isDefault: '1' }).then((resp) => {
    if (resp && resp.success && resp.data[0]) {
      formInfo.value.data.receiveProvince = resp.data[0].province
      formInfo.value.data.receiveCity = resp.data[0].city
      formInfo.value.data.receiveCounty = resp.data[0].county
      formInfo.value.data.receiveAddress = resp.data[0].address
      formInfo.value.data.receiveAddressValue = resp.data[0].addressValue
      formInfo.value.data.receiveContacts = resp.data[0].contacts
      formInfo.value.data.receivePhone = resp.data[0].contactsPhone
    }
  })
})

setData()
function setData() {
  let obj = {
    qty: {
      label: '申购数量',
      value: 'qty',
      type: 'slot',
      minWidth: '150px',
      hidden: true,
    },
    price: {
      label: '单价',
      value: 'price',
      type: 'slot',
      minWidth: '150px',
      permission: !proxy.$permission('assetPurchOrder:price'),
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('assetPurchOrder:price'),
      hidden: true,
    },
    assetApplicationNo: {
      label: '资产申购论证号',
      value: 'assetApplicationNo',
      type: 'slot',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
function changeLarge(e, str) {
  proxy.$refs.tableForm.clearValidate(str)
}
function ifRender() {
  return !(
    formInfo.value.data.contractId && formInfo.value.data.contractId != -2
  )
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//添加产品按钮
function addProduct() {
  if (tableInfo.value.data.length) {
    selectEquipmentInfo.value.query.queryMap.id.value = tableInfo.value.data
      .map((item) => item.productId)
      .toString()
  } else {
    selectEquipmentInfo.value.query.queryMap.id.value = ''
  }
  selectEquipmentInfo.value.visible = true
}
function addContractProduct() {
  if (tableInfo.value.data.length) {
    selectContractDetailInfo.value.query.queryMap.productId.value =
      tableInfo.value.data.map((item) => item.productId).toString()
  } else {
    selectContractDetailInfo.value.query.queryMap.productId.value = ''
  }
  if (props.orderId || props.id) {
    selectContractDetailInfo.value.query.queryParams.purchOrderId =
      props.orderId || props.id
  }
  selectContractDetailInfo.value.query.queryMap.orderId.value =
    formInfo.value.data.contractId
  selectContractDetailInfo.value.visible = true
}
function callBackEquipment(rows) {
  if (rows && rows.length) {
    const data = rows.map(function (item) {
      return {
        ...item,
        productId: item.id,
        productCode: item.code,
        qty: undefined,
        price: item.price || undefined,
      }
    })
    tableInfo.value.data = data.concat(tableInfo.value.data)
  }
}
//删除产品
function removeProduct(row = undefined) {
  if (row.productId && row) {
    tableInfo.value.data = tableInfo.value.data.filter((item) => row !== item)
  } else {
    proxy
      .$confirm('删除后将改变数据,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        tableInfo.value.data = tableInfo.value.data.filter(
          (row) => !tableInfo.value.selections.includes(row)
        )
      })
  }
}
//打开选择收货地址窗口
function openReceiveAddressWindow() {
  selectReceiveAddressInfo.value.visible = true
}
function selectReceiveAddressCallback(data) {
  if (data) {
    formInfo.value.data.receiveAddressValue = data.addressValue
    formInfo.value.data.receiveProvince = data.province
    formInfo.value.data.receiveCity = data.city
    formInfo.value.data.receiveCounty = data.county
    formInfo.value.data.receiveAddress = data.address
    formInfo.value.data.receiveContacts = data.contacts
    formInfo.value.data.receivePhone = data.contactsPhone
  }
}
function selectSupplierCallback(data) {
  tableInfo.value.data[index.value] = {
    ...tableInfo.value.data[index.value],
    supplierId: data.supplierId,
    supplierName: data.supplierName,
  }
}
function changeAllSupplier() {
  allSupplierInfo.value.visible = true
}
function selectAllSupplierCallback(data) {
  tableInfo.value.data.forEach((item) => {
    item['supplierId'] = data.supplierId
    item['supplierName'] = data.supplierName
  })
}
function showAssetApplicationDetail(row) {
  assetApplicationDetailInfo.value.id = row.assetApplicationId
  assetApplicationDetailInfo.value.visible = true
}
//切换供应商
function changeSupplier(row, _index) {
  console.log(row)
  index.value = _index
  supplierInfo.value.query.productId = row.productId
  supplierInfo.value.query.supplierIdNotIn = row.supplierId
  supplierInfo.value.visible = true
}
function addAssetApplication(row, _index) {
  index.value = _index
  if (tableInfo.value.data && tableInfo.value.data.length > 0) {
    selectAssetApplicationInfo.value.query.queryMap.id.value =
      tableInfo.value.data.map((item) => item.assetApplicationId).toString()
  } else {
    selectAssetApplicationInfo.value.query.queryMap.id.value = ''
  }
  if (props.orderId || props.id) {
    selectAssetApplicationInfo.value.query.queryParams.purchOrderId =
      props.orderId || props.id
  }
  ;(selectAssetApplicationInfo.value.query.queryParams.productId =
    row.productId),
    (selectAssetApplicationInfo.value.visible = true)
}
function callBackContractDetail(rows) {
  if (rows && rows.length) {
    console.log(rows)
    const data = rows.map(function (item) {
      return {
        ...item,
        productId: item.productId,
        productCode: item.productCode,
        qty: item.pendPurchQty,
        pendPurchQty: item.pendPurchQty,
        price: item.price,
        supplierId: item.supplierId,
        supplierName: item.supplierName,
      }
    })
    tableInfo.value.data = data.concat(tableInfo.value.data)
  }
}
function callBackAssetApplication(data) {
  if (data && data.id) {
    tableInfo.value.data[index.value] = {
      ...tableInfo.value.data[index.value],
      assetApplicationId: data.id,
      assetApplicationNo: data.orderNo,
      qty: data.qty,
      price: data.price,
      pendPurchQty: data.qty - data.prePurchQty,
    }
  } else {
    tableInfo.value.data[index.value] = {
      ...tableInfo.value.data[index.value],
      assetApplicationId: -2,
      assetApplicationNo: '',
      pendPurchQty: undefined,
    }
  }
}
//打开部门弹窗
function openDeptWindow() {
  selectDeptInfo.value.visible = true
}
function selectDeptCallback(data) {
  if (data) {
    if (formInfo.value.data.deptId !== data.id) {
      formInfo.value.data.deptName = data.name
      formInfo.value.data.deptId = data.id
    }
  } else {
    formInfo.value.data.deptName = ''
    formInfo.value.data.deptId = ''
  }
}
// flowData 外部流程页面调用
function saveAndSubmit(flowData) {
  save(true, flowData)
}
function save(isSaveAndSubmit, obj = {}) {
  formRefs.value = [proxy.$refs['tableForm'], formInfo.value.ref]
  proxy.$fn.checkForm(formRefs.value).then(() => {
    const details = tableInfo.value.data.map((item) => {
      return {
        ...item,
        calcQty: item.qty,
      }
    })
    let commonName = details
      .filter((item) => !item.supplierId)
      .map((item) => item.commonName)
    if (commonName.length > 0) {
      proxy.$message({
        duration: 3500,
        type: 'warning',
        showClose: true,
        message: `产品${commonName}供应商不能为空!`,
      })
    } else {
      const data = formInfo.value.data
      data.isSpecialEdit = props.isSpecialEdit
      data.assetDetails = details
      if (data.assetDetails.length < 1) {
        proxy.$message({
          duration: 3500,
          type: 'warning',
          showClose: true,
          message: '产品不能为空',
        })
        return
      }
      const api = data.id
        ? updateAssetPurchOrder
        : isSaveAndSubmit === true
        ? createAssetPurchOrderAndSave
        : createAssetPurchOrder
      if (isSaveAndSubmit === true) {
        let flowParams = {
          orderData: data,
          // flowTaskCommentDto和processDefinitionKey为审批流程需要传递的参数
          flowTaskCommentDto: { approvalType: 'submit' },
          processDefinitionKey: 'assetPurchOrder',
          processInstanceId: obj.processInstanceId,
          taskId: obj.taskId,
        }
        if (!proxy.$route.query.processInstanceId) {
          api(flowParams).then((resp) => {
            if (resp && resp.success) {
              emits('returnModify', false)
              showDialog.value = false
            }
          })
          return
        }
        methodAssetPurchOrder(flowParams, 'flowSubmit').then((resp) => {
          if (resp && resp.success) {
            emits('returnModify', false)
            showDialog.value = false
          }
        })
      } else {
        api(data).then((resp) => {
          if (resp && resp.success) {
            emits('returnModify', false)
            showDialog.value = false
          }
        })
      }
    }
  })
}
function _getAssetPurchOrderById(orderId) {
  getAssetPurchOrderById(orderId).then((resp) => {
    if (resp && resp.success) {
      formInfo.value.data = resp.data
      getAssetPurchOrderDetails()
    }
  })
}
function getAssetPurchOrderDetails() {
  getAssetPurchOrdersDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
        tableInfo.value.data.forEach((item) => {
          item.supplierId = formInfo.value.data.supplierId
          item.supplierName = formInfo.value.data.supplierName
        })
      }
    }
  )
}
function getAssetPurchOrderDetailByContractId() {
  getAssetPurchOrdersDetailByContractId(props.contractId).then((resp) => {
    if (resp && resp.success) {
      tableInfo.value.data = resp.data
    }
  })
}
function tableRowClassName({ row = undefined }) {
  if (row.licenseEndDate) {
    let dataDiff = proxy.$fn.dateDiffLicen(row.licenseEndDate)
    if (dataDiff <= 0) {
      return 'expire-row'
    } else if (Number(licencesSystemConfig.value) >= dataDiff) {
      return 'nearly-row'
    }
  }
  return ''
}
function getCustomize() {
  let param = {
    tableKey:
      props.tableKey ||
      'assetPurchOrderDetail-com.slip.spd.entity.vo.asset.AssetPurchOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )

      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap.orderId.value = props.orderId || props.id
      if (props.orderId || props.id) {
        _getAssetPurchOrderById(props.orderId || props.id)
      } else if (props.contractId && props.contractId != -2) {
        getAssetPurchOrderDetailByContractId(props.contractId)
      }
    }
  })
}
// 名称不要做改变，在流程引擎中需要通过refs进行调用
defineExpose({
  save,
  saveAndSubmit,
})
</script>

<style scoped lang="scss">
:deep(.table-scroll-hidden) > .el-table {
  overflow-y: hidden;
}
.model-panel-title {
  display: flex;
  align-items: center;

  .model-tips {
    display: flex;
    align-items: center;
    margin-left: 10px;
    color: #909399;

    .model-tips-nearly,
    .model-tips-expire {
      display: block;
      margin-left: 5px;
      width: 17px;
      height: 17px;
    }

    .model-tips-nearly {
      background: #fceacb;
    }

    .model-tips-expire {
      background: #f5e4e4;
    }
  }
}

:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
